package com.kovalko.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;

import static org.testng.Assert.assertTrue;

/**
 * Created by 4uvak on 04.07.2014.
 */
public class TicketsPage extends BasePage {

    private static final String adultsTicketsLocator = "//div[@class='adults_block passengers_wrapper']/div[@class='persons']/div[%d]";
    private static final String childrenTicketsLocator = "//div[@data-field='children passengers_wrapper']/div[@class='persons']/div[%d]";
    private static final String infantsTicketsLocator = "//div[@class='infants_block passengers_wrapper']/div[@class='persons']/div[%d]";
    private static final By errorPopup = By.className("error_popup");
    private static final By findBtn = By.id("start_search");

    private static final String TICKETS_PAGE_URL = "http://rozetka.com.ua/travel/IEV/DOK";

    public TicketsPage(WebDriver driver) {
        super(driver, TICKETS_PAGE_URL);
    }

    public void buyTickets(int adults, int children, int infants) {
        Assert.assertTrue(adults >= 0 & children >= 0 & infants >= 0, "Amount of people can't be negative: [" + adults + "," + children + "," + infants + "]");
        //init page
        driver.findElement(By.xpath(String.format(adultsTicketsLocator, 1))).click();

        //test
        if (adults > 0) {
            driver.findElement(By.xpath(String.format(adultsTicketsLocator, adults))).click();
        }
        if (children > 0) {
            driver.findElement(By.xpath(String.format(childrenTicketsLocator, children))).click();
        }
        if (infants > 0) {
            driver.findElement(By.xpath(String.format(infantsTicketsLocator, infants))).click();
        }
        driver.findElement(findBtn).click();
    }

    public boolean isError() {
        return driver.findElements(errorPopup).size() > 0;
    }

}

